const { getPool } = require('../../../config/db')

const HanziStroke = {
  async getAllHanziStrokes() {
    const mysqlPool = await getPool()
    const [rows] = await mysqlPool.execute('SELECT * FROM hanzi_stroke')
    return rows
  },

  async getHanziStrokeById(id) {
    const mysqlPool = await getPool()
    const [rows] = await mysqlPool.execute('SELECT * FROM hanzi_stroke WHERE id = ?', [id])
    return rows[0]
  },

  async createHanziStroke(strokeData) {
    const mysqlPool = await getPool()
    const [result] = await mysqlPool.execute(
      'INSERT INTO hanzi_stroke (hanzi_id, sort_order, audio_path, duration, image_path) VALUES (?, ?, ?, ?, ?)',
      [strokeData.hanzi_id, strokeData.sort_order, strokeData.audio_path, strokeData.duration, strokeData.image_path]
    )
    return result.insertId
  },

  async updateHanziStroke(id, strokeData) {
    const mysqlPool = await getPool()
    await mysqlPool.execute(
      'UPDATE hanzi_stroke SET hanzi_id=?, sort_order=?, audio_path=?, duration=?, image_path=? WHERE id=?',
      [strokeData.hanzi_id, strokeData.sort_order, strokeData.audio_path, strokeData.duration, strokeData.image_path, id]
    )
  },

  async deleteHanziStroke(id) {
    const mysqlPool = await getPool()
    await mysqlPool.execute('DELETE FROM hanzi_stroke WHERE id = ?', [id])
  },
}

module.exports = HanziStroke